var app = getApp()
Page({
  data:{
    current: 0,
    cakes:[{
      name:"草莓奶油蛋糕",
      price:"239",
      introduce:"6寸 淡奶蛋糕 黄色 红色小猪 数字蜡烛 贺卡1",
      num:"1",
    }],
    price:[{
      money:"2.00",
    }],
    conse:[{
      name:"王学东",
      telephone:"15950676265",
      address:"江苏省徐州市鼓楼区淮海文化产业园",
    }],
    reser:[{
      name:"王东东",
      tel:"151521659888",
      manner:"配送上门",
      time:"当日15:20",
      remarks:"多送一盒叉子",
    }],
    freight:[{
      price:"0.00",
      payment:"239",
    }],
    bill:[{
      data:"2017-09-20 15：30：20",
      serial:"64645646563",
    }],
    proges:[{
      now1:'订单关闭',
      now2:'退款完成'
    }]
  },
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh')
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    var that = this;
  
  },
  //详情页跳转
  lookdetail:function(e){
    var lookid=e.currentTarget.dataset;
    console.log(e.currentTarget.dataset.id);
    wx.navigateTo({
      url:"/pages/yiguo/detail/detail?id="+lookid.id
    })
  },
  switchSlider:function(e){
    this.setData({
      current:e.target.dataset.index
    })
  },
  changeSlider:function(e){
    this.setData({
      current: e.detail.current
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },

  onLoad:function(option){
    var id = option.ide;
    var that = this;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/order/order_info',
      data: {
        id: id,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        console.log(res);
        that.setData({
          orders: res.data.orders,
        })
      },
    })
  },

  //退款
  tuikuan:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/yiguo/refund/refund?id=" + id,
    })
  },
  //取消订单
  quxiao:function(e){
    var id = e.currentTarget.dataset.id;
    var uid = app.whereId.uid;
    wx.showModal({
      title: '提示',
      content: '确认取消该订单？',
      success: function (res) {
        wx.request({
          url: 'https://www.cakeyouxuan.com/small/order/quxiao',
          data: {
            id: id,
            uid: uid,
            tag: 1,
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"  //post
          },
          success: function (res) {
            if (res.data.msg == 'T'){
              wx.showToast({
                title: '订单已取消',
                icon: 'success',
                duration: 2000,
                success: function () {
                  setTimeout(function () {
                    wx.navigateBack({
                      delta: 2
                    })
                  }, 2000) //延迟时间
                }
              })
            }else{
              wx.showToast({
                title: '操作失败',
                icon: 'success',
                duration: 2000,
                success: function () {
                }
              })
            }
          },
        })
      }
    })
  },

  //确认收货
  shouhuo:function(e){
    var id = e.currentTarget.dataset.id;
    var uid = app.whereId.uid;
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/order/shouhuo',
      data: {
        id: id,
        uid: uid,
        tag: 3,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"  //post
      },
      success: function (res) {
        if (res.data.msg == 'T') {
          wx.showToast({
            title: '收货成功',
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.navigateBack({
                  delta: 2
                })
              }, 2000) //延迟时间
            }
          })
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'success',
            duration: 2000,
            success: function () {
            }
          })
        }
      },
    })
  },

  //评价
  pingjia:function(e){
    this.data.navigateToNextDelta = true
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/yiguo/comment/comment?ide=" + id,
    })
  },

  //提醒发货
  tx_fahuo:function(){
    wx.showToast({
      title: '已提醒商家',
      icon: 'success',
      duration: 2000,
      success: function () {
      }
    })
  },

//拨打电话
  calphone:function(e){
    var phone = e.currentTarget.dataset.id;
    wx.makePhoneCall({
      phoneNumber: phone 
    })
  },

  //确认支付
  payfor:function(e){
    var id = e.currentTarget.dataset.id;  //订单id
    var openid = app.globalData.openid; //openid
    wx.request({
      url: 'https://www.cakeyouxuan.com/small/payin/index',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      data: {
        id: id,
        openid: openid,
      },
      success: function (res) {
        console.log(res);
        if (res.data.state == 1) {
          // --------- 订单生成成功，发起支付请求 ------------------
          wx.requestPayment({
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,   //字符串随机数
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            'success': function (res) {
              //requestPayment:ok==>调用支付成功
              if (res.errMsg == "requestPayment:ok") {
                wx.request({
                  url: 'https://www.cakeyouxuan.com/small/payin/notifys',
                  data: {
                    id: id,
                  },
                  method: "POST",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"  //post
                  },
                  success: function (res) {
                    wx.navigateTo({
                      url: "/pages/yiguo/success/success?price=" + res.data.price + "&id=" + res.data.ids,
                    })
                  },
                })
              }
            },
            'fail': function (res) {

            },
            'complete': function (res) {

            }
          })
        } else if (res.data.state == 0) {
          wx.showToast({
            title: res.data.Msg,
            icon: 'fail',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '系统繁忙，请稍后重试~',
            icon: 'fail',
            duration: 1000
          })
        }
      }

    }) 
  },
})
